package com.ruoyi.sgs.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.sgs.domain.SgsHero;
import com.ruoyi.sgs.service.ISgsHeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 武将Controller
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/hero")
public class SgsHeroController extends BaseController
{
    @Autowired
    private ISgsHeroService sgsHeroService;

    /**
     * 查询武将列表
     */
    @RequiresPermissions("sgs:hero:list")
    @GetMapping("/list")
    public TableDataInfo list(SgsHero sgsHero)
    {
        startPage();
        List<SgsHero> list = sgsHeroService.selectSgsHeroList(sgsHero);
        return getDataTable(list);
    }

    /**
     * 导出武将列表
     */
    @RequiresPermissions("sgs:hero:export")
    @Log(title = "武将", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgsHero sgsHero)
    {
        List<SgsHero> list = sgsHeroService.selectSgsHeroList(sgsHero);
        ExcelUtil<SgsHero> util = new ExcelUtil<SgsHero>(SgsHero.class);
        util.exportExcel(response, list, "武将数据");
    }

    /**
     * 获取武将详细信息
     */
    @RequiresPermissions("sgs:hero:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sgsHeroService.selectSgsHeroById(id));
    }

    /**
     * 新增武将
     */
    @RequiresPermissions("sgs:hero:add")
    @Log(title = "武将", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SgsHero sgsHero)
    {
        return toAjax(sgsHeroService.insertSgsHero(sgsHero));
    }

    /**
     * 修改武将
     */
    @RequiresPermissions("sgs:hero:edit")
    @Log(title = "武将", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgsHero sgsHero)
    {
        return toAjax(sgsHeroService.updateSgsHero(sgsHero));
    }

    /**
     * 删除武将
     */
    @RequiresPermissions("sgs:hero:remove")
    @Log(title = "武将", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sgsHeroService.deleteSgsHeroByIds(ids));
    }

    @Log(title = "武将导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SgsHero> util = new ExcelUtil<SgsHero>(SgsHero.class);
        List<SgsHero> userList = util.importExcel(file.getInputStream());
        String message = sgsHeroService.importHeros(userList, updateSupport);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SgsHero> util = new ExcelUtil<SgsHero>(SgsHero.class);
         util.importTemplateExcel(response,"武将数据");
    }

    @PostMapping("/heroesPrice")
    @Log(title = "武将估价", businessType = BusinessType.OTHER)
    public R heroesPrice(@RequestBody Map<String,String> map)
    {
        String price =sgsHeroService.heroesPrice(map);
        return R.ok(price);
    }
}
